<template>
	<div class="comment-list">
		<van-list
		  v-model="loading"
		  :finished="finished"
		  finished-text="没有更多了"
		  @load="onLoad"
		>
			
		  <comment-item v-for="(item,index) in list" :key="index" :comment="item" @reply-click="$emit('reply-click',$event)" />
		</van-list>
	</div>
</template>

<script>
	import { getComments } from '../../../utils/comment.js'
	import CommentItem from './comment-item.vue'
	
	export default {
		name:'CommentList',
		components:{
			CommentItem
		},
		props:{
			source: {
				type:[Number,String,Object],
				required:true
			},
			type: {
				type:String,
				default: 'a'
			},
			list: {
				type:Array,
				default() {
					return []
				}
			}
		},
		data() {
			return {
				// list: [],
				loading: false,
				finished: false,
				offset:null,
				limit:10
			}
		},
		methods:{
			async onLoad() {
			  const { data } = await getComments({
					type:this.type,
					source:this.source.toString(),
					offset:this.offset,
					limit:this.limit
				})
				this.$emit('update-total-count',data.data.total_count)
				const { results } = data.data
				this.list.push(...results)
				
				
				this.loading = false
				
				if(results.length) {
					this.offset = data.data.last_id
				}else {
					this.finished = true
				}
			 },
		}
	}
</script>

<style>
</style>
